﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using NHibernate;
using Common;
using Educational.Model.Entity;
using Educational.Model.EntityManager;

namespace Educational.Business
{
    public class PositionAdmin
    {
        #region DeletePosition删除一个职位
        /// <summary>
        /// 删除一个职位
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="positionId"></param>
        public static void DeletePosition(int schoolId, int positionId)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                collision.itemKey = "DeletePosition" + positionId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);

                Position position = PositionManager.Get2(session, schoolId, positionId);
                if (position == null)
                    throw new Exception("不存在这个职位或者这个职位已经被删除！");
                if (position.teachers.Count > 0)
                    throw new Exception("当前职位存在教师与其关联，请先移除教师关联！");
                PositionManager.Delete(session, position);

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region AddPosition添加一个职位
        /// <summary>
        /// 添加一个职位
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="departmentId"></param>
        /// <param name="schoolRoleGroupId"></param>
        /// <param name="positionName"></param>
        public static void AddPosition(int schoolId, int departmentId, int schoolRoleGroupId, string positionName)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                collision.itemKey = "DeleteSchool" + schoolId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);
                Basic.Model.Entity.Collision collision1 = new Basic.Model.Entity.Collision();
                collision1.itemKey = "DeleteDepartment" + departmentId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision1);
                Basic.Model.Entity.Collision collision2 = new Basic.Model.Entity.Collision();
                collision2.itemKey = "DeleteSchoolRoleGroup" + schoolRoleGroupId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision2);

                Position position = new Position();
                position.department = DepartmentManager.Get1(session, schoolId, departmentId);
                if (position.department == null)
                    throw new Exception("职位必须属于一个部门，不允许将职位直接添加到学校！");
                position.schoolRoleGroup = SchoolRoleGroupManager.Get1(session, schoolId, schoolRoleGroupId);
                position.positionName = positionName;
                PositionManager.Add(session, position);

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision1);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision2);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region UpdatePosition修改一个职位
        /// <summary>
        /// 修改一个职位
        /// </summary>
        /// <param name="positionId"></param>
        /// <param name="schoolId"></param>
        /// <param name="departmentId"></param>
        /// <param name="schoolRoleGroupId"></param>
        /// <param name="positionName"></param>
        public static void UpdatePosition(int positionId, int schoolId, int departmentId, int schoolRoleGroupId, string positionName)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                collision.itemKey = "DeleteSchool" + schoolId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);
                Basic.Model.Entity.Collision collision1 = new Basic.Model.Entity.Collision();
                collision1.itemKey = "DeleteDepartment" + departmentId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision1);
                Basic.Model.Entity.Collision collision2 = new Basic.Model.Entity.Collision();
                collision2.itemKey = "DeleteSchoolRoleGroup" + schoolRoleGroupId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision2);
                Basic.Model.Entity.Collision collision3 = new Basic.Model.Entity.Collision();
                collision3.itemKey = "DeletePosition" + positionId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision3);

                Position position = PositionManager.Get1(session, schoolId, positionId);
                position.department = DepartmentManager.Get1(session, schoolId, departmentId);
                if (position.department == null)
                    throw new Exception("部门不能为空！");
                position.schoolRoleGroup = SchoolRoleGroupManager.Get1(session, schoolId, schoolRoleGroupId);
                position.positionName = positionName;

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision1);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision2);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision3);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region GetsPosition获取职位列表，根据分页
        /// <summary>
        /// 获取职位列表，根据分页
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="departmentId"></param>
        /// <param name="rowIndex"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static PageResult<Educational.VO.Position> GetsPosition(int schoolId, int departmentId, int rowIndex, int rowCount)
        {
            IList<Position> entityData = null;
            PageResult<Educational.VO.Position> pageResult = new PageResult<Educational.VO.Position>();
            pageResult.data = new List<Educational.VO.Position>();
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                IList<Department> departmentList = DepartmentManager.Gets1(session, schoolId);
                IList<int> departmentSelfAndChildIds = new List<int>();
                departmentSelfAndChildIds.Add(departmentId);
                DepartmentAdmin.GetChildDepartmentIds(departmentList, departmentSelfAndChildIds, departmentId);
                entityData = PositionManager.Gets1(session, schoolId, departmentSelfAndChildIds.ToArray<int>(), rowIndex, rowCount);
                pageResult.totalCount = PositionManager.Gets1Count(session, schoolId, departmentSelfAndChildIds.ToArray<int>());
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            for (int i = 0; i < entityData.Count; i++)
            {
                Educational.VO.Position position = new Educational.VO.Position();
                position.positionId = entityData[i].positionId;
                position.positionName = entityData[i].positionName;
                if (entityData[i].schoolRoleGroup != null)//填充权限组
                {
                    position.schoolRoleGroup = new Educational.VO.SchoolRoleGroup();
                    position.schoolRoleGroup.roleGroupId = entityData[i].schoolRoleGroup.roleGroupId;
                    position.schoolRoleGroup.roleGroupName = entityData[i].schoolRoleGroup.roleGroupName;
                }
                if (entityData[i].teachers != null)//填充教师列表
                {
                    position.teachers = new List<Educational.VO.Teacher>();
                    IEnumerator<Teacher> teachers = entityData[i].teachers.GetEnumerator();
                    while (teachers.MoveNext())
                    {
                        Educational.VO.Teacher teacher = new Educational.VO.Teacher();
                        teacher.teacherId = teachers.Current.teacherId;
                        teacher.teacherName = teachers.Current.teacherName;
                        position.teachers.Add(teacher);
                    }
                }
                position.department = new Educational.VO.Department();
                position.department.departmentId = entityData[i].department.departmentId;
                position.department.departmentName = entityData[i].department.departmentName;
                pageResult.data.Add(position);
            }
            return pageResult;
        }
        #endregion
    }
}
